package com.caifu.util;

import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.UUID;

/**
 * @Auther: Lyf
 * @Date: 2020/7/8 09:36
 * @Description:
 */
@Component
@PropertySource(value = {"classpath:definition.properties"},encoding = "utf-8")
public class FileUpload {

    @Value("${file.upload.path}")
    private String fileUploadPath;
    @Value("${file.upload.result.path}")
    private String fileUploadResultPath;

    private static String uploadPath;
    private static String uploadResultPath;

    public static JSONObject uploadFile(MultipartFile file, String path){
        JSONObject obj = new JSONObject();
        try{
            String fileName = file.getOriginalFilename();
            String newFileName = getNewFileName(fileName);

            String now = Util.dateToString(null,"yyyyMMdd");

            String newPath = uploadPath + now + "/" + path + "/" + newFileName;
            String resultPath = uploadResultPath + now + "/" + path + "/" + newFileName;

            File mkdir = new File(newPath);
            if(!mkdir.exists()){
                mkdir.mkdirs();
            }
            newPath = newPath + "/" + newFileName;
            File newFile = new File(newPath);

            file.transferTo(newFile);

            obj.put("path",resultPath);
            obj.put("name",fileName);
            obj.put("code","success");
        }catch (Exception e){
            obj.put("code","error");
            e.printStackTrace();
            throw new RuntimeException("图片上传失败！");
        }
        return obj;
    }

    public static String upload(MultipartFile file, String path){
        try{
            String fileName = file.getOriginalFilename();
            String newFileName = getNewFileName(fileName);

            String now = Util.dateToString(null,"yyyyMMdd");

            String newPath = uploadPath + now + "/" + path ;
            String resultPath = uploadResultPath + now + "/" + path + "/" + newFileName;

            File mkdir = new File(newPath);
            if(!mkdir.exists()){
                mkdir.mkdirs();
            }
            newPath = newPath + "/" + newFileName;
            File newFile = new File(newPath);

            file.transferTo(newFile);
            return resultPath;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("图片上传失败！");
        }
    }

    /**
     * 功能描述: 根据uuid生成不重复的文件名
     *
     * @auther: Lyf
     * @date: 2020/7/20 10:26
     * @param:
     * @return:
     */
    public static String getNewFileName(String fileName){
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        return UUID.randomUUID().toString().replace("-","") + suffix;
    }

    public static void main(String[] args) {
        System.out.println(uploadPath);
    }

    @PostConstruct
    private void init(){
        uploadPath = fileUploadPath;
        uploadResultPath = fileUploadResultPath;
    }
}
